#include "Test2.h"

Test2 Test2::m_Test2;

sf::Vector2f buttonSize3 = sf::Vector2f(150,25);
sf::Color buttonColor3 = sf::Color::White;
int mouseTest = 0;
sf::Vector2i position = sf::Vector2i(0,0);
sf::Vector2i boxSize2;
sf::RectangleShape mouseRect;

int spriteAlpha = 0;
int transitionTime = 500;
bool timerOn = false;

void Test2::Init(sf::RenderWindow& window){

        if(!spTexture.loadFromFile("resources/mom_player.png")){
        std::cout << " Texture did not load";
    }
    sprite.setTexture(spTexture);
    sprite.setTextureRect(sf::IntRect(0,0,64,64));
    sprite.setPosition(sf::Vector2f(200,100));
    sprite.setColor(sf::Color(255,255,255,spriteAlpha));
    sprite.setScale(5.0, 5.0);

    // ++ Clock ++
    clock.restart();

    // +++ VIEWS +++
    view.setCenter(window.getSize().x / 2, window.getSize().y / 2);
    view.setSize(window.getSize().x, window.getSize().y);
    window.setView(view);

    // ++ TGUI STUFF ++
    gui.setWindow(window);
    gui.setGlobalFont("resources/arial.ttf");

    //child window
    tgui::ChildWindow::Ptr cWindow(gui);
    cWindow->load("resources/Black.conf");
    cWindow->setSize(200,50);
    cWindow->setPosition(600,50);

    // button
    tgui::Button::Ptr button2(*cWindow);
    button2->load("resources/Black.conf");
    button2->setSize(buttonSize3.x, buttonSize3.y);
    button2->setPosition(25, 10);
    button2->setText("Return to Menu");
    button2->bindCallback(tgui::Button::LeftMouseClicked);
    button2->setCallbackId(1);
    button2->setTextColor(buttonColor3);

        //Return to menu button
    tgui::Button::Ptr button(gui);
    button->load("resources/Black.conf");
    button->setSize(150, 25);
    button->setPosition(25, 50);
    button->setText("Disappear");
    button->bindCallback(tgui::Button::LeftMouseClicked);
    button->setCallbackId(2);
    button->setTextColor(buttonColor3);


        // EditBox1 - X coordinate
    tgui::EditBox::Ptr editBox1(gui);
    editBox1->load("resources/Black.conf");
    editBox1->setSize(50,30);
    editBox1->setPosition(200, 10);
    editBox1->bindCallback(tgui::Button::LeftMouseClicked);
    editBox1->setCallbackId(5);
    editBox1->setText("5");
    c3 = editBox1;



    // ++ RECTANGLE ++
    rectangle1.setPosition(sf::Vector2f(400,300));
    rectangle1.setSize(sf::Vector2f(200,200));
    rectangle1.setFillColor(sf::Color::Blue);

    //create a rectangle WHITE rectangle with a green border using retangle1

    r2.setPosition(sf::Vector2f(rectangle1.getGlobalBounds().left, rectangle1.getGlobalBounds().top));
    r2.setSize(sf::Vector2f(rectangle1.getGlobalBounds().width, rectangle1.getGlobalBounds().height));
    r2.setFillColor(sf::Color::Transparent);
    r2.setOutlineColor(sf::Color::Green);
    r2.setOutlineThickness(2);


    // +++ TEXT +++
    text1.setPosition(sf::Vector2f(200,200));
    text1.setString("This is a good string test");


    sf::CircleShape shape(50);

    shape.setPosition(sf::Vector2f(100,200));

}

void Test2::Cleanup(){}

void Test2::Pause(){}
void Test2::Resume(){}

void Test2::Events(GameEngine* game, sf::RenderWindow& window){

        while (window.pollEvent(event))
        {


            gui.handleEvent(event);

            // The callback loop
            tgui::Callback callback;
            while (gui.pollCallback(callback)){

                // Return to introState
                if (callback.id == 1){
                    game->ChangeState(IntroState::Instance() );
                }

                // Return to introState
                if (callback.id == 2){
                    std::cout << "Resetting!";
                    spriteAlpha = 0;
                    sprite.setColor(sf::Color(255,255,255,spriteAlpha));
                    std::cout << clock.getElapsedTime().asSeconds() << std::endl;
                    clock.restart();
                    timerOn = true;
                }

            }


            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed){
                game->Quit();
            }

            //grow and shrink the sprite using the mouse scrollwheel
/*
            if (event.type == sf::Event::MouseWheelMoved){
                if(event.mouseWheel.delta >= 1){
                    x_view *= 1.1;
                    y_view *= 1.1;
                }else if (event.mouseWheel.delta <= -1){
                    x_view *= .95;
                    y_view *= .95;

                }
                sprite.setTextureRect(sf::IntRect(0,0,x_view,y_view));
            }
*/

            //Right click to check for mouse in object
            if (event.type == sf::Event::MouseButtonPressed){

/*
                if(event.mouseButton.button == sf::Mouse::Left){
                    position = sf::Mouse::getPosition(window);
                    std::cout << "X0: " << position.x << " Y0: " << position.y << std::endl;
                }
*/

                //rotate rectangle
                if(event.mouseButton.button == sf::Mouse::Right){
                    // get the current mouse position in the window
                    sf::Vector2i pixelPos = sf::Mouse::getPosition(window);
                    // convert it to world coordinates
                    sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);

                    // check to see if the mouse is inside the rectangle
                    if(rectangle1.getGlobalBounds().contains(worldPos)){
                        if(rectangle1.getFillColor() == sf::Color::Red){
                            rectangle1.setFillColor(sf::Color::Blue);
                        }else rectangle1.setFillColor(sf::Color::Red);
                    }

                }
            }

/*
            //control continuous mouse clicks
            if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
                sf::Vector2i position2 = sf::Mouse::getPosition(window);
                //std::cout << "X1: " << position2.x << " Y1: " << position2.y << std::endl;
                //boxSize = sf::Vector2i((position2.x - position.x), (position2.y - position.y));
                boxSize2 = position2 - position;
                std::cout << "Box_X: " << boxSize2.x << " Box_Y: " << boxSize2.y << std::endl;
            }
*/

            //mouse button released
            /*
            if (event.type == sf::Event::MouseButtonReleased){

                if(event.mouseButton.button == sf::Mouse::Left){
                    boxSize2 = sf::Vector2i(0,0);
                    std::cout << "RESET: " << "Box_X: " << boxSize2.x << " Box_Y: " << boxSize2.y << std::endl;
                }
            }
            */


           //other method for keyboard events
            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Right)
                {
                    r1Rotation += 10;
                    rectangle1.setRotation(r1Rotation);
                    r2.setPosition(sf::Vector2f(rectangle1.getGlobalBounds().left, rectangle1.getGlobalBounds().top));
                    r2.setSize(sf::Vector2f(rectangle1.getGlobalBounds().width, rectangle1.getGlobalBounds().height));
                }

                if (event.key.code == sf::Keyboard::Space){
                    view.zoom(0.5f);
                    std::cout << "You pressed space" << std::endl;
                }
            }


        }

}

void Test2::Update(GameEngine* game, sf::RenderWindow& window){

    //std::cout << clock.getElapsedTime().asMicroseconds() << std::endl;
    //clock.restart();


    //reduce the alpha of the sprite if the timer is on
    if(timerOn){


        if (clock.getElapsedTime().asMilliseconds() >= transitionTime / 255){
            if(spriteAlpha <=254 ){
                spriteAlpha += 1;
                clock.restart();
                std::cout << "Alpha: " << spriteAlpha << std::endl;
                sprite.setColor(sf::Color(255,255,255,spriteAlpha));
            } else{
                std::cout << "Done";
                timerOn = false;
            }

        }


    }

}

void Test2::Render(GameEngine* game, sf::RenderWindow& window){

    // clear the window with black color
    window.clear(sf::Color::White);

    //window.draw(map);

    //window.draw(text1);

    gui.draw();

    //window.draw(shape);
    //window.draw(rectangle1);
    //window.draw(r2);

    window.draw(sprite);

    mouseRect.setPosition((sf::Vector2f)position);
    mouseRect.setSize((sf::Vector2f)boxSize2);
    mouseRect.setFillColor(sf::Color::Transparent);
    mouseRect.setOutlineColor(sf::Color::Green);
    mouseRect.setOutlineThickness(2);
    window.draw(mouseRect);

    window.display();

}
